<template>
  <div id="app">
    <!-- 头部 -->
    <header>
      <span class="logo">Analysis</span>
      <div class="logout">
        {{
          userInfo.info && userInfo.info.username ? userInfo.info.username : ""
        }}
        <el-button @click="logout">退出登录</el-button>
      </div>
    </header>
    <!-- 侧边菜单 -->
    <el-container>
      <el-aside width="200px">
        <el-menu :default-active="activeIndex" router :default-openeds="opens">
          <template v-for="(menu, index) in menu">
            <el-submenu
              :index="index.toString()"
              :key="index"
              v-if="menu.children"
            >
              <template slot="title">
                <span>{{ menu.title }}</span>
              </template>
              <el-menu-item
                v-for="(subMenu, subIndex) in menu.children.filter(
                  (child) => !child.hidden
                )"
                :key="subIndex"
                :index="
                  subMenu.path
                    .replace(/\:.*\?/, '')
                    .replace(ROTUE_PREFIX_LOCAL, '')
                "
              >
                {{ subMenu.title }}
              </el-menu-item>
            </el-submenu>
            <el-menu-item :index="menu.path" v-else :key="index">
              {{ menu.title }}
            </el-menu-item>
          </template>
        </el-menu>
      </el-aside>
      <el-main><router-view></router-view></el-main>
    </el-container>
  </div>
</template>
<script>
import { ROUTE_PREFIX } from "@/store/index.js";
export default {
  data() {
    return {
      // 去拿token的路由
      setTokenUrl: `${window.location.origin}/settoken`,
      activeIndex: "/",
      ROTUE_PREFIX_LOCAL: ROUTE_PREFIX,
      // 当前打开的菜单
      opens:['0'],
    };
  },
  watch: {
    // 在下面的$setMenus会设置路由，就会触发这里的监听
    asyncRoute: {
      handler(newVal, oldVal) {
        // 这个addRoutes要求参数是数组
        this.$router.addRoutes(newVal);
      },
    },
  },
  computed: {
    // userInfo () {
    //   return this.$store.getters.$userInfo
    // }
    // cdn引入的情况下这样写
    ...Vuex.mapGetters({
      userInfo: "$userInfo",
      menu: "$menu",
      asyncRoute:'$asyncRoute'
    }),
  },
  methods: {
    init() {
      this.$service
        .AuthInfo()
        .then((res) => {
          // 存得到的数据
          this.$store.dispatch("$setUserInfo", res.user);
          this.$store.dispatch("$setMenus", res.menus);
        })
        .catch((err) => {
          // 去登录啦(通过这里去拿MIS的token)
          window.location.href = `${
            process.env.VUE_APP_LOGIN_SITE
          }?redirect=${encodeURIComponent(this.setTokenUrl)}`;
        });
    },
    // 退出登录
    logout() {
      this.$store.dispatch("$setToken", "");
      window.location.href = `${
        process.env.VUE_APP_LOGIN_SITE
      }logout?redirect=${encodeURIComponent(this.setTokenUrl)}`;
    },
  },
  mounted() {
    this.init();
  },
};
</script>
<style lang="less">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
}

header {
  height: 80px;
  width: 100%;
  background-color: #324057;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #fff;
  .logo {
    font-size: 30px;
    padding-left: 30px;
  }
  .logout {
    padding-right: 30px;
    .el-button {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #eee;
  min-height: calc(100vh - 80px);
}

.el-menu {
  height: 100%;
  text-align: left;
}
</style>
